// 可选参数
function buildName(firstName: string, lastName?: string) { // 可选参数需要在 必选参数后面
  if (lastName)
      return firstName + " " + lastName;
  else
      return firstName;
}

let result1 = buildName("Bob");  // works correctly now
let result2 = buildName("Bob", "Adams", "Sr.");  // error, too many parameters
let result3 = buildName("Bob", "Adams");  // ah, just right


// 默认参数
// lastName有默认参数，与可选参数类型一致，默认位置在必选参数后
function buildName1(firstName: string, lastName = "Smith") {
  return firstName + " " + lastName;
}

let result11 = buildName1("Bob");                  // works correctly now, returns "Bob Smith"
let result21 = buildName1("Bob", undefined);       // still works, also returns "Bob Smith"
let result31 = buildName1("Bob", "Adams", "Sr.");  // error, too many parameters
let result41 = buildName1("Bob", "Adams");         // ah, just right


// 默认参数在前
// 需要传undefined或者赋值
function buildName2(firstName = "Will", lastName: string) {
  return firstName + " " + lastName;
}

let result12 = buildName2("Bob");                  // error, too few parameters
let result22 = buildName2("Bob", "Adams", "Sr.");  // error, too many parameters
let result32 = buildName2("Bob", "Adams");         // okay and returns "Bob Adams"
let result42 = buildName2(undefined, "Adams");     // okay and returns "Will Adams"

